﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.LIB;
using YArchitech.Revit;
using YArchitech.Revit.Electric.Common;
using YArchitech.Revit.RevitExtension;

namespace HYRElectric.PowerSystem
{
	public class SystemDiagramOperate
	{
		public bool Execute(UIDocument uiDocument)
		{
			this.m_UIDoc = uiDocument;
			this.m_Doc = this.m_UIDoc.Document;
			Transaction transaction = new Transaction(this.m_Doc);
			transaction.Start("CreateViewDrafting");
			ViewDrafting viewDrafting = this.CreateViewDrafting();
			if (viewDrafting == null)
			{
				MessageBox.Show("创建绘图视图失败！");
				transaction.RollBack();
				return false;
			}
			viewDrafting.get_Parameter(BuiltInParameter.VIEW_NAME).Set("盈建科电气系统图");
			transaction.Commit();
			this.m_UIDoc.ActiveView = viewDrafting;
			Transaction transaction2 = new Transaction(this.m_Doc);
			transaction2.Start("ExportSystemDiagram");
			if (this.m_UIDoc.ActiveView.Scale != 100)
			{
				this.m_UIDoc.ActiveView.Scale = 100;
			}
			ExportSystemXML exportSystemXML = new ExportSystemXML(this.m_SystemGraphPath);
			this.m_ListCSystemDataBlock = exportSystemXML.LoadSystemDataBlock();
			if (this.m_ListCSystemDataBlock.Count == 0)
			{
				MessageBox.Show("读取电气系统数据出错！");
				return false;
			}
			if (!File.Exists(this.m_SystemGraphSetPath))
			{
				this.WriteXml(this.m_SystemGraphSetPath);
			}
			ExportSystemXML exportSystemXML2 = new ExportSystemXML(this.m_SystemGraphSetPath);
			this.m_SystemGraphSet = exportSystemXML2.LoadCSystemGraphSet(this.m_UIDoc.ActiveView.Scale);
			if (this.m_SystemGraphSet == null)
			{
				MessageBox.Show("读取系统图设置数据出错！");
				return false;
			}
			Color color = new Color(0, byte.MaxValue, 0);
			this.m_SwitchLength = AssistFunc.mmToFeet((double)this.m_SystemGraphSet.InLink) * 0.75;
			this.m_MidPoint = XYZ.Zero;
			List<SystemDataBlock> list = new List<SystemDataBlock>();
			List<SystemDataBlock> list2 = new List<SystemDataBlock>();
			foreach (SystemDataBlock systemDataBlock in this.m_ListCSystemDataBlock)
			{
				if (systemDataBlock.NDataType == 0)
				{
					list.Add(systemDataBlock);
				}
				else
				{
					list2.Add(systemDataBlock);
				}
			}
			for (int i = 0; i < list.Count; i++)
			{
				this.m_SpareLoopPhase = "";
				this.m_Curves.Clear();
				this.m_Lables.Clear();
				this.m_CurveColors.Clear();
				List<SystemDataBlock> list3 = new List<SystemDataBlock>();
				foreach (SystemDataBlock systemDataBlock2 in list2)
				{
					if (systemDataBlock2.StrParentGUID == list[i].StrGUID)
					{
						list3.Add(systemDataBlock2);
					}
				}
				for (int j = 0; j < list3.Count - 1; j++)
				{
					for (int k = 0; k < list3.Count - 1 - j; k++)
					{
						if (Convert.ToInt32(list3[k].StrNum) > Convert.ToInt32(list3[k + 1].StrNum))
						{
							SystemDataBlock value = list3[k];
							list3[k] = list3[k + 1];
							list3[k + 1] = value;
						}
					}
				}
				if (list3.Count > 0)
				{
					this.GetLoopSwitchNoteWidth(list3);
					if (list3.Count <= this.m_SystemGraphSet.LoopNum)
					{
						this.m_SpareLoop = this.m_SystemGraphSet.MinSpareLoop;
					}
					else
					{
						this.m_SpareLoop = this.m_SystemGraphSet.MaxSpareLoop;
					}
					int count = list3.Count;
					int spareLoop = this.m_SpareLoop;
					try
					{
						if (i > 0)
						{
							this.m_MidPoint = new XYZ(this.m_EndPoint.X + AssistFunc.mmToFeet((double)(this.m_SystemGraphSet.InLine + this.m_SystemGraphSet.InLink + this.m_SystemGraphSet.LoopSpace)) + this.m_SwitchLength, this.m_MidPoint.Y, this.m_MidPoint.Z);
						}
						this.DrawInLine(list[i]);
						this.DrawBusLoop(list3);
						this.DrawRectangle();
						this.DrawTextNote(this.m_RectTopLeft, list[i]);
						List<ElementId> list4 = new List<ElementId>();
						ElementSet elementSet = new ElementSet();
						foreach (ElementId elementId in this.m_Curves)
						{
							elementSet.Insert(this.m_Doc.GetElement(elementId));
							list4.Add(elementId);
						}
						foreach (ElementId elementId2 in this.m_Lables)
						{
							elementSet.Insert(this.m_Doc.GetElement(elementId2));
							list4.Add(elementId2);
						}
						ElementId item = null;
						foreach (ElementId elementId3 in this.m_Curves)
						{
							if (this.m_Bus.Id == elementId3)
							{
								item = elementId3;
								break;
							}
						}
						this.m_Doc.Regenerate();
						Group group = this.m_Doc.CreatYJKGroup(elementSet);
						this.m_Curves.Remove(item);
						this.m_UIDoc.ActiveView.SetYJKProjLineWeightOverrideByElement(this.m_Curves, (int)this.m_SystemGraphSet.WireWidth);
						this.m_UIDoc.ActiveView.SetYJKProjColorOverrideByElement(this.m_CurveColors, color);
						if (this.m_SystemGraphSet.Vector == 1)
						{
							Line line = YJKLineEx.YJKGetBound(this.m_MidPoint, new XYZ(this.m_MidPoint.X, this.m_MidPoint.Y, this.m_MidPoint.Z + 1.0));
							ElementTransformUtils.RotateElement(this.m_Doc, group.Id, line, -Math.PI*.5);
						}
					}
					catch (Exception ex)
					{
						MessageBox.Show(ex.Message);
						transaction2.Commit();
						return false;
					}
				}
			}
			transaction2.Commit();
			return true;
		}

		private void GetLoopSwitchNoteWidth(List<SystemDataBlock> systemData)
		{
			List<double> list = new List<double>();
			List<double> list2 = new List<double>();
			foreach (SystemDataBlock systemDataBlock in systemData)
			{
				string str = "3P";
				if (systemDataBlock.StrPhase != "3P")
				{
					str = "1P";
				}
				string name = systemDataBlock.StrSwitchType + systemDataBlock.StrSwitchSpec + "A/" + str;
                TextNote textNote = this.DrawText(XYZ.Zero, (TextAlignFlags)2048, name);
				BoundingBoxXYZ boundingBoxXYZ = textNote.get_BoundingBox(this.m_Doc.ActiveView);
				double item = Math.Abs(boundingBoxXYZ.Max.X - boundingBoxXYZ.Min.X);
				list.Add(item);
                TextNote textNote2 = this.DrawText(XYZ.Zero, (TextAlignFlags)2048, systemDataBlock.Prefix + systemDataBlock.StrNum);
				boundingBoxXYZ = textNote2.get_BoundingBox(this.m_Doc.ActiveView);
				item = Math.Abs(boundingBoxXYZ.Max.X - boundingBoxXYZ.Min.X);
				list2.Add(item);
				this.m_Doc.DeleteElement(textNote);
				this.m_Doc.DeleteElement(textNote2);
			}
			this.m_SwitchNoteWidth = list.Max();
			this.m_LoopNumWidth = list2.Max();
		}

		private AnnotationSymbolType GetSwitchType(Document m_Doc, string switchName)
		{
			List<AnnotationSymbolType> list = (from r in (from ele in RevitFilter.GetElementsOfType(m_Doc, typeof(FamilySymbol)).OfCategory(BuiltInCategory.OST_GenericAnnotation)
			select (AnnotationSymbolType)ele).ToList<AnnotationSymbolType>()
			where r.Name == switchName
			select r).ToList<AnnotationSymbolType>();
			if (list.Count == 0)
			{
				if (!this.HYSwitchFamily(m_Doc, switchName))
				{
					throw new Exception("加载配件族失败!");
				}
				list = (from r in (from ele in RevitFilter.GetElementsOfType(m_Doc, typeof(FamilySymbol)).OfCategory(BuiltInCategory.OST_GenericAnnotation)
				select (AnnotationSymbolType)ele).ToList<AnnotationSymbolType>()
				where r.Name == switchName
				select r).ToList<AnnotationSymbolType>();
			}
			return list[0];
		}

		public bool HYSwitchFamily(Document doc, string switchName)
		{
			string text = Path.Combine(Product.FamilyLocation, RevitVersion.GetVersionNumber(doc), "SystemGraphSwitch", switchName + ".rfa");
			return doc.LoadFamily(text);
		}

		public ViewDrafting CreateViewDrafting()
		{
			ViewDrafting viewDrafting = null;
			foreach (Element element in RevitFilter.GetElementsOfType(this.m_Doc, typeof(ViewDrafting)).OfCategory(BuiltInCategory.OST_Views).ToList<Element>())
			{
				if (ExtensibleStorageManager.ReadExtensibleStorageFromElement(element))
				{
					viewDrafting = (element as ViewDrafting);
					this.DeleteViewDraftingElements(viewDrafting);
					return viewDrafting;
				}
			}
			if (viewDrafting == null)
			{
				int scale = 100;
				if (!this.CreateViewDrafting(this.m_Doc, scale, out viewDrafting))
				{
					return viewDrafting;
				}
				ExtensibleStorageManager.WriteExtensibleStorageToElement(ref viewDrafting, "ElectricalSystemDiagram", "SystemDiagramName");
			}
			return viewDrafting;
		}

		private void DeleteViewDraftingElements(ViewDrafting viewDrafting)
		{
			ICollection<ElementId> collection = RevitFilter.GetElementsOfType(this.m_Doc, viewDrafting.Id, typeof(Group)).OfCategory(BuiltInCategory.OST_IOSDetailGroups).ToElementIds();
			this.m_Doc.Delete(collection);
			ICollection<ElementId> collection2 = RevitFilter.GetElementsOfType(this.m_Doc, viewDrafting.Id, typeof(CurveElement)).OfCategory(BuiltInCategory.OST_Lines).ToElementIds();
			if (collection2.Count > 0)
			{
				this.m_Doc.Delete(collection2);
			}
			ICollection<ElementId> collection3 = RevitFilter.GetElementsOfType(this.m_Doc, viewDrafting.Id, typeof(TextNote)).OfCategory(BuiltInCategory.OST_TextNotes).ToElementIds();
			if (collection3.Count > 0)
			{
				this.m_Doc.Delete(collection3);
			}
			ICollection<ElementId> collection4 = RevitFilter.GetElementsOfType(this.m_Doc, viewDrafting.Id, typeof(FamilyInstance)).OfCategory(BuiltInCategory.OST_GenericAnnotation).ToElementIds();
			if (collection4.Count > 0)
			{
				this.m_Doc.Delete(collection4);
			}
		}

		public bool CreateViewDrafting(Document m_Doc, int scale, out ViewDrafting view)
		{
			view = RevitViewExtension.CreateViewDrafting(m_Doc);
			if (view == null)
			{
				return false;
			}
			BoundingBoxXYZ boundingBoxXYZ = new BoundingBoxXYZ();
			boundingBoxXYZ.Max = new XYZ(10.0, 10.0, 5.0);
			boundingBoxXYZ.Min = new XYZ(-10.0, -10.0, -5.0);
			view.CropBox = boundingBoxXYZ;
			view.Scale = scale;
			view.ApplyViewTemplateParameters(m_Doc.ActiveView);
			return true;
		}

		private void DrawRectangle()
		{
			List<ElementId> list = new List<ElementId>();
			double length = (this.m_Bus.Location as LocationCurve).Curve.Length;
			double num = this.m_FontHeight * 7.0;
			double num2;
			if (length < num)
			{
				num2 = num;
			}
			else
			{
				num2 = length / 2.0 + AssistFunc.mmToFeet((double)this.m_SystemGraphSet.LoopSpace);
			}
			double num3;
			if (this.m_SwitchNoteWidth > this.m_SwitchLength)
			{
				num3 = this.m_SwitchNoteWidth;
			}
			else
			{
				num3 = this.m_SwitchLength + this.m_SwitchLength / 2.0;
			}
			double num4 = AssistFunc.mmToFeet((double)this.m_SystemGraphSet.InLink);
			double num5 = AssistFunc.mmToFeet((double)this.m_SystemGraphSet.OutLink);
			this.m_RectTopLeft = new XYZ(this.m_MidPoint.X - num4 - num3, this.m_MidPoint.Y + num2, this.m_MidPoint.Z);
			XYZ xyz = new XYZ(this.m_MidPoint.X + num5 + num3, this.m_MidPoint.Y + num2, this.m_MidPoint.Z);
			XYZ xyz2 = new XYZ(this.m_MidPoint.X - num4 - num3, this.m_MidPoint.Y - num2, this.m_MidPoint.Z);
			XYZ ptEnd = new XYZ(this.m_MidPoint.X + num5 + num3, this.m_MidPoint.Y - num2, this.m_MidPoint.Z);
			DetailCurve detailCurve = this.DrawModelLine(this.m_RectTopLeft, xyz);
			list.Add(detailCurve.Id);
			this.m_Curves.Add(detailCurve.Id);
			detailCurve = this.DrawModelLine(xyz2, ptEnd);
			list.Add(detailCurve.Id);
			this.m_Curves.Add(detailCurve.Id);
			detailCurve = this.DrawModelLine(this.m_RectTopLeft, xyz2);
			list.Add(detailCurve.Id);
			this.m_Curves.Add(detailCurve.Id);
			detailCurve = this.DrawModelLine(xyz, ptEnd);
			list.Add(detailCurve.Id);
			this.m_Curves.Add(detailCurve.Id);
			this.m_UIDoc.ActiveView.SetYJKProjLinePatternOverrideByElement(list, LinePatternElement.GetLinePatternElementByName(this.m_Doc, "划线"));
		}

		private void DrawTextNote(XYZ origin, SystemDataBlock panelData)
		{
			double num = AssistFunc.mmToFeet((double)this.m_SystemGraphSet.InLink);
			double num2 = 1.2;
			double num3 = origin.X + num2;
			DetailCurve detailCurve = this.DrawModelLine(new XYZ(num3, origin.Y - this.m_FontHeight, origin.Z), new XYZ(origin.X + num2 + num, origin.Y - this.m_FontHeight, origin.Z));
			XYZ origin2 = (new XYZ(num3, origin.Y - this.m_FontHeight, origin.Z) + new XYZ(origin.X + num2 + num, origin.Y - this.m_FontHeight, origin.Z)) / 2.0;
			string text = Math.Round(panelData.DPe / 1000.0, 2).ToString();
            TextNote textNote = this.DrawText(origin2, (TextAlignFlags)2176, panelData.StrNum);
            TextNote textNote2 = this.DrawText(origin2, (TextAlignFlags)640, text + "KW");
            TextNote textNote3 = this.DrawText(new XYZ(num3, this.m_MidPoint.Y + this.m_FontHeight, origin.Z), (TextAlignFlags)2048, "Ic=" + panelData.DIc.ToString() + "A");
            TextNote textNote4 = this.DrawText(new XYZ(num3, this.m_MidPoint.Y + this.m_FontHeight * 2.0, origin.Z), (TextAlignFlags)2048, "Cosφ=" + panelData.DCos.ToString());
            TextNote textNote5 = this.DrawText(new XYZ(num3, this.m_MidPoint.Y + this.m_FontHeight * 3.0, origin.Z), (TextAlignFlags)2048, "Kx=" + panelData.DKx.ToString());
            TextNote textNote6 = this.DrawText(new XYZ(num3, this.m_MidPoint.Y + this.m_FontHeight * 4.0, origin.Z), (TextAlignFlags)2048, "Pe=" + text + "KW");
			this.m_Curves.Add(detailCurve.Id);
			this.m_Lables.Add(textNote.Id);
			this.m_Lables.Add(textNote2.Id);
			this.m_Lables.Add(textNote6.Id);
			this.m_Lables.Add(textNote5.Id);
			this.m_Lables.Add(textNote4.Id);
			this.m_Lables.Add(textNote3.Id);
		}

		private void DrawInLine(SystemDataBlock panelData)
		{
			double num = AssistFunc.mmToFeet((double)this.m_SystemGraphSet.InLink);
			double num2 = AssistFunc.mmToFeet((double)this.m_SystemGraphSet.InLine);
			DetailCurve detailCurve = this.DrawModelLine(this.m_MidPoint, new XYZ(this.m_MidPoint.X - num, this.m_MidPoint.Y, this.m_MidPoint.Z));
			this.DrawSwitch(new XYZ(this.m_MidPoint.X - num - this.m_SwitchLength, this.m_MidPoint.Y, this.m_MidPoint.Z), panelData);
			DetailCurve detailCurve2 = this.DrawModelLine(new XYZ(this.m_MidPoint.X - num - this.m_SwitchLength, this.m_MidPoint.Y, this.m_MidPoint.Z), new XYZ(this.m_MidPoint.X - num - this.m_SwitchLength - num2, this.m_MidPoint.Y, this.m_MidPoint.Z));
			this.m_Curves.Add(detailCurve.Id);
			this.m_Curves.Add(detailCurve2.Id);
		}

		private void DrawSwitch(XYZ origin, SystemDataBlock systemData)
		{
			string str = "3P";
			if (systemData.StrPhase != "3P")
			{
				str = "1P";
			}
			string name = systemData.StrSwitchType + systemData.StrSwitchSpec + "A/" + str;
            TextNote textNote = this.DrawText(origin, (TextAlignFlags)2048, name);
			BoundingBoxXYZ boundingBoxXYZ = textNote.get_BoundingBox(this.m_Doc.ActiveView);
			this.m_FontHeight = Math.Abs(boundingBoxXYZ.Max.Y - boundingBoxXYZ.Min.Y);
			AnnotationSymbolType switchType = this.GetSwitchType(this.m_Doc, "断路器");
			AnnotationSymbol annotationSymbol = this.m_Doc.Create.NewFamilyInstance(origin, switchType, this.m_Doc.ActiveView) as AnnotationSymbol;
			this.m_Lables.Add(textNote.Id);
			this.m_CurveColors.Add(annotationSymbol.Id);
			this.m_Curves.Add(annotationSymbol.Id);
		}

		private void DrawBusLoop(List<SystemDataBlock> listLoopData)
		{
			int count = listLoopData.Count;
			double num = AssistFunc.mmToFeet((double)this.m_SystemGraphSet.LoopSpace);
			double num2 = num * 0.5;
			double num3 = num * (double)(count + this.m_SpareLoop - 1) / 2.0 + num2;
			this.m_Bus = this.DrawModelLine(new XYZ(this.m_MidPoint.X, this.m_MidPoint.Y + num3, this.m_MidPoint.Z), new XYZ(this.m_MidPoint.X, this.m_MidPoint.Y - num3, this.m_MidPoint.Z));
			List<ElementId> list = new List<ElementId>();
			list.Add(this.m_Bus.Id);
			this.m_UIDoc.ActiveView.SetYJKProjLineWeightOverrideByElement(list, (int)this.m_SystemGraphSet.BusWidth);
			this.m_CurveColors.Add(this.m_Bus.Id);
			this.m_Curves.Add(this.m_Bus.Id);
			for (int i = 0; i < listLoopData.Count; i++)
			{
				this.DrawLoop(new XYZ(this.m_MidPoint.X, this.m_MidPoint.Y + num3 - num2 - (double)i * num, this.m_MidPoint.Z), listLoopData[i]);
			}
			for (int j = 0; j < this.m_SpareLoop; j++)
			{
				int num4 = listLoopData.Count + j;
				this.DrawSpareLoop(new XYZ(this.m_MidPoint.X, this.m_MidPoint.Y + num3 - num2 - (double)num4 * num, this.m_MidPoint.Z), listLoopData.Count + 1 + j, listLoopData[0]);
			}
		}

		private void DrawLoop(XYZ origin, SystemDataBlock systemData)
		{
			double num = AssistFunc.mmToFeet((double)this.m_SystemGraphSet.OutLink);
			double num2 = AssistFunc.mmToFeet((double)this.m_SystemGraphSet.OutLine);
			double num3 = AssistFunc.mmToFeet((double)this.m_SystemGraphSet.InLink);
            TextNote textNote = this.DrawText(origin, (TextAlignFlags)2048, systemData.StrPhase);
			textNote.get_Parameter(BuiltInParameter.TEXT_ALIGN_HORZ).Set(256);
			DetailCurve detailCurve = this.DrawModelLine(origin, new XYZ(origin.X + num, origin.Y, origin.Z));
			this.DrawSwitch(new XYZ(origin.X + num, origin.Y, origin.Z), systemData);
			DetailCurve detailCurve2 = this.DrawModelLine(new XYZ(origin.X + num + this.m_SwitchLength, origin.Y, origin.Z), new XYZ(origin.X + num + this.m_SwitchLength + num2, origin.Y, origin.Z));
			double num4 = AssistFunc.mmToFeet(150.0);
			double num5;
			if (this.m_SwitchNoteWidth > this.m_SwitchLength)
			{
				num5 = origin.X + num + this.m_SwitchNoteWidth + num4;
			}
			else
			{
				num5 = origin.X + num + this.m_SwitchLength + this.m_SwitchLength / 2.0 + num4;
			}
            TextNote textNote2 = this.DrawText(new XYZ(num5, origin.Y, origin.Z), (TextAlignFlags)2048, systemData.Prefix + systemData.StrNum);
			BoundingBoxXYZ boundingBoxXYZ = textNote2.get_BoundingBox(this.m_UIDoc.ActiveView);
			Math.Abs(boundingBoxXYZ.Max.X - boundingBoxXYZ.Min.X);
            TextNote textNote3 = this.DrawText(new XYZ(num5 + this.m_LoopNumWidth + num4 * 2.0, origin.Y, origin.Z), (TextAlignFlags)2048, string.Concat(new object[]
			{
				systemData.StrWireType,
				"-",
				systemData.NWireNum.ToString(),
				"×",
				systemData.DWireS.ToString(),
				" ",
				systemData.StrPipeType,
				systemData.DPipeDia,
				" ",
				systemData.StrLaying
			}));
			double num6 = origin.X + num + this.m_SwitchLength + num2 + this.m_SwitchLength;
			DetailCurve detailCurve3 = this.DrawModelLine(new XYZ(num6, origin.Y, origin.Z), new XYZ(num6 + num3, origin.Y, origin.Z));
			XYZ origin2 = (new XYZ(num6, origin.Y, origin.Z) + new XYZ(num6 + num3, origin.Y, origin.Z)) / 2.0;
            TextNote textNote4 = this.DrawText(origin2, (TextAlignFlags)2176, systemData.StrLoadType);
            TextNote textNote5 = this.DrawText(origin2, (TextAlignFlags)640, Math.Round(systemData.DPe / 1000.0, 2).ToString() + "KW");
			this.m_EndPoint = new XYZ(origin.X + num + this.m_SwitchLength + num2 + this.m_SwitchLength + num3, origin.Y, origin.Z);
			this.m_Lables.Add(textNote.Id);
			this.m_Curves.Add(detailCurve.Id);
			this.m_Curves.Add(detailCurve2.Id);
			this.m_Curves.Add(detailCurve3.Id);
			this.m_Lables.Add(textNote2.Id);
			this.m_Lables.Add(textNote3.Id);
			this.m_Lables.Add(textNote4.Id);
			this.m_Lables.Add(textNote5.Id);
		}

		private void DrawSpareLoop(XYZ origin, int index, SystemDataBlock systemData)
		{
			double num = AssistFunc.mmToFeet((double)this.m_SystemGraphSet.OutLink);
			double num2 = AssistFunc.mmToFeet((double)this.m_SystemGraphSet.OutLine);
			double num3 = AssistFunc.mmToFeet((double)this.m_SystemGraphSet.InLink);
			if (this.m_SpareLoopPhase == "")
			{
				this.m_SpareLoopPhase = "A";
			}
			else if (this.m_SpareLoopPhase == "A")
			{
				this.m_SpareLoopPhase = "B";
			}
			else if (this.m_SpareLoopPhase == "B")
			{
				this.m_SpareLoopPhase = "C";
			}
			else if (this.m_SpareLoopPhase == "C")
			{
				this.m_SpareLoopPhase = "A";
			}
            TextNote textNote = this.DrawText(origin, (TextAlignFlags)2048, this.m_SpareLoopPhase);
			textNote.get_Parameter(BuiltInParameter.TEXT_ALIGN_HORZ).Set(256);
			DetailCurve detailCurve = this.DrawModelLine(origin, new XYZ(origin.X + num, origin.Y, origin.Z));
			this.DrawSwitch(new XYZ(origin.X + num, origin.Y, origin.Z), systemData);
			DetailCurve detailCurve2 = this.DrawModelLine(new XYZ(origin.X + num + this.m_SwitchLength, origin.Y, origin.Z), new XYZ(origin.X + num + this.m_SwitchLength + num2, origin.Y, origin.Z));
			double num4 = AssistFunc.mmToFeet(150.0);
			double num5;
			if (this.m_SwitchNoteWidth > this.m_SwitchLength)
			{
				num5 = origin.X + num + this.m_SwitchNoteWidth + num4;
			}
			else
			{
				num5 = origin.X + num + this.m_SwitchLength + this.m_SwitchLength / 2.0 + num4;
			}
            TextNote textNote2 = this.DrawText(new XYZ(num5, origin.Y, origin.Z), (TextAlignFlags)2048, systemData.Prefix + index.ToString());
			double num6 = origin.X + num + this.m_SwitchLength + num2 + this.m_SwitchLength;
			DetailCurve detailCurve3 = this.DrawModelLine(new XYZ(num6, origin.Y, origin.Z), new XYZ(num6 + num3, origin.Y, origin.Z));
			XYZ origin2 = (new XYZ(num6, origin.Y, origin.Z) + new XYZ(num6 + num3, origin.Y, origin.Z)) / 2.0;
            TextNote textNote3 = this.DrawText(origin2, (TextAlignFlags)2176, "备用");
            TextNote textNote4 = this.DrawText(origin2, (TextAlignFlags)640, "-");
			this.m_Lables.Add(textNote.Id);
			this.m_Curves.Add(detailCurve.Id);
			this.m_Curves.Add(detailCurve2.Id);
			this.m_Curves.Add(detailCurve3.Id);
			this.m_Lables.Add(textNote2.Id);
			this.m_Lables.Add(textNote3.Id);
			this.m_Lables.Add(textNote4.Id);
		}

		private TextNote DrawText(XYZ origin, TextAlignFlags textAlignFlags, string name)
		{
			double num = AssistFunc.mmToFeet(this.m_SystemGraphSet.TextHeight);
			double lineWidth = (double)this.GetLength(name) * num;
			TextNote textNode = this.m_Doc.GetTextNode(this.m_UIDoc.ActiveView, origin, new XYZ(1.0, 0.0, 0.0), new XYZ(0.0, 1.0, 0.0), lineWidth, textAlignFlags, name);
			TextNoteType textNoteType = this.m_Doc.GetElement(textNode.GetTypeId()) as TextNoteType;
			textNoteType.get_Parameter(BuiltInParameter.TEXT_SIZE).Set(num);
			textNoteType.get_Parameter(BuiltInParameter.TEXT_BACKGROUND).Set(1);
			textNoteType.get_Parameter(BuiltInParameter.LEADER_OFFSET_SHEET).Set(0);
			textNode.get_Parameter(BuiltInParameter.KEEP_READABLE).Set(0);
			return textNode;
		}

		private DetailCurve DrawModelLine(XYZ ptStart, XYZ ptEnd)
		{
			Line line = YJKLineEx.YJKGetBound(ptStart, ptEnd);
			return this.m_Doc.Create.NewDetailCurve(this.m_UIDoc.ActiveView, line);
		}

		private int GetLength(string str)
		{
			if (str.Length == 0)
			{
				return 0;
			}
			Encoding encoding = new ASCIIEncoding();
			int num = 0;
			byte[] bytes = encoding.GetBytes(str);
			for (int i = 0; i < bytes.Length; i++)
			{
				if (bytes[i] == 63)
				{
					num += 2;
				}
				else
				{
					num++;
				}
			}
			return num;
		}

		private void WriteXml(string path)
		{
			XmlDocument xmlDocument = new XmlDocument();
			XmlDeclaration newChild = xmlDocument.CreateXmlDeclaration("1.0", "UTF-8", null);
			xmlDocument.AppendChild(newChild);
			XmlElement newChild2 = xmlDocument.CreateElement("Boot");
			xmlDocument.AppendChild(newChild2);
			XmlNode xmlNode = xmlDocument.SelectSingleNode("Boot");
			XmlElement xmlElement = xmlDocument.CreateElement("HYSystemGrapSet");
			XmlElement xmlElement2 = xmlDocument.CreateElement("InLine");
			xmlElement2.SetAttribute("InLine", "60");
			xmlElement2.SetAttribute("InLink", "20");
			xmlElement.AppendChild(xmlElement2);
			XmlElement xmlElement3 = xmlDocument.CreateElement("SystemLoop");
			xmlElement3.SetAttribute("OutLine", "90");
			xmlElement3.SetAttribute("OutLink", "20");
			xmlElement3.SetAttribute("LoopSpace", "15");
			xmlElement3.SetAttribute("LoopNum", "5");
			xmlElement3.SetAttribute("MinSpareLoop", "1");
			xmlElement3.SetAttribute("MaxSpareLoop", "2");
			xmlElement.AppendChild(xmlElement3);
			XmlElement xmlElement4 = xmlDocument.CreateElement("Other");
			xmlElement4.SetAttribute("BusWidth", "6");
			xmlElement4.SetAttribute("WireWidth", "4");
			xmlElement4.SetAttribute("TextHeight", "3.5");
			xmlElement4.SetAttribute("Vector", "0");
			xmlElement.AppendChild(xmlElement4);
			xmlNode.AppendChild(xmlElement);
			xmlDocument.Save(path);
		}

		private UIDocument m_UIDoc;

		private Document m_Doc;

		private List<ElementId> m_Curves = new List<ElementId>();

		private List<ElementId> m_Lables = new List<ElementId>();

		private List<ElementId> m_CurveColors = new List<ElementId>();

		private List<SystemDataBlock> m_ListCSystemDataBlock;

		private SystemGraphSet m_SystemGraphSet;

		private string m_SystemGraphSetPath = Path.Combine(Product.WorkingLocation, "SystemGraphSet.xml");

		private string m_SystemGraphPath = Path.Combine(Product.WorkingLocation, "SystemGraph.xml");

		private int m_SpareLoop;

		private string m_SpareLoopPhase = "";

		private DetailCurve m_Bus;

		private XYZ m_MidPoint;

		private double m_SwitchLength;

		private XYZ m_RectTopLeft;

		private XYZ m_EndPoint;

		private double m_FontHeight;

		private double m_SwitchNoteWidth;

		private double m_LoopNumWidth;
	}
}
